G_PICTURE_DELETE_LINK = null;
G_PICTURE_RENAME_LINK = null;

$(function()
{
	// supprimer une vidéo
	$('a.js-del-video').click(function() {

		// BUG: changer le contenu HTML empêche de supprimer la ligne du tableau (tr)
		//$(this).parent().html('<img src="images/ajax-loader.gif" alt="Suppression..." />');
		
		var id = $(this).attr('id').split('_')[1];
		var self = this;
		$.ajax({
			url: 'pages_admin/ajax_admin.php',
			type: 'GET',
			data: 'action=video_delete'
				+ '&id=' + id,
			
			success: function(result) {
				$(self).closest('tr').animate({
					'backgroundColor': '#ff8888',
					'color': '#941010'
				}, 500);
				$(self).closest('tr').fadeOut();
				update_cpt('#count_video', -1);
			}
		});
	});
	
	// supprimer une galerie
	$('a.js-del-gallery').click(function() {
		var gallery = $(this).attr('id');
		var self = this;
		$.ajax({
			url: 'pages_admin/ajax_admin.php',
			type: 'GET',
			data: 'action=gallery_delete'
				+ '&gallery=' + gallery,
			
			success: function(result) {
				result = result.split('&&&');
				if (result[0] == 'ok') {
					$(self).closest('tr').animate({
						'backgroundColor': '#ff8888',
						'color': '#941010'
					}, 500);
					$(self).closest('tr').fadeOut();
					update_cpt('#count_gallery', -1);
					// le nombre de photos supprimées est retournée
					update_cpt('#count_photo', -result[1]);
				}
				else {
					alert('Erreur lors de la suppresion de la gallerie');
				}
			}
		});
	});
	
	// action supprimer une photo
	$('a.js-del-photo').click(function() {
		G_PICTURE_DELETE_LINK = this;
		$('#dialog-delete-picture').dialog('open');
	});
	
	// popup supprimer une photo
	$("#dialog-delete-picture").dialog({
		autoOpen: false,
		resizable: false,
		modal: true,
		title: "Supprimer la photo",
		buttons: {
			'Valider': function() {
				var gallery = $('#gallery_name').val();
				var photo = $(G_PICTURE_DELETE_LINK).closest('.picture-item').find('.photo_name').html();
				$.ajax({
					url: 'pages_admin/ajax_admin.php',
					type: 'GET',
					data: 'action=photo_delete'
						+ '&gallery=' + gallery
						+ '&photo=' + photo,
			
					success: function(result) {
						$('#dialog-delete-picture').dialog('close');
						update_cpt('#count_photo', -1);
						$(G_PICTURE_DELETE_LINK).closest('.picture-item').fadeOut();
					},
					error: function(result) {
						alert('Impossible de supprimer la photo !');
					}
				});
			},
			'Annuler': function() {
				$(this).dialog('close');
			}
		}
	});

	// action renommer une photo
	$('a.js-rename-photo').click(function() {
		G_PICTURE_RENAME_LINK = this;
		$('#dialog-rename-picture').dialog('open');
	});

	// popup renommer une photo
	$("#dialog-rename-picture").dialog({
		autoOpen: false,
		resizable: false,
		modal: true,
		width: 450,
		title: "Renommer la photo",
		open: function() {
			var photo = $(G_PICTURE_RENAME_LINK).closest('.picture-item').find('.photo_name').html();
			$('#input-rename-picture').attr('value', photo);
			$('#input-rename-picture').focus();
		},
		buttons: {
			'Valider': function() {
				$('#dialog-rename-picture .ajax-loader').html('<img src="images/ajax-loader-min.gif" alt="En cours..." />');
				
				var gallery_name = $('#gallery_name').val();
				var old_pic_name = $(G_PICTURE_RENAME_LINK).closest('.picture-item').find('.photo_name').html();
				var new_pic_name = $('#input-rename-picture').val();
			
				$.ajax({
					url: 'pages_admin/ajax_admin.php',
					type: 'POST',
					data: 'action=photo_update_name'
						+ '&gallery_name=' + gallery_name
						+ '&old_pic_name=' + old_pic_name
						+ '&new_pic_name=' + new_pic_name,
			
					success: function(result) {
						$('#dialog-rename-picture .ajax-loader').html('');
						$('#dialog-rename-picture').dialog('close');
						$(G_PICTURE_RENAME_LINK).closest('.picture-item').find('.photo_name').html(new_pic_name);
					},
					error: function(result) {
						alert('Impossible de renommer la photo !');
					}
				});
			},
			'Annuler': function() {
				$(this).dialog('close');
			}
		}
	});
});

